/*
 * Copyright (c) 2018-2024 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "js.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief       policy: check load schedule threshold
 *
 * @param[in]   load     #1: node current load
 * @param[in]   thresh   #2: queue or host defined load schedule threshold
 * @param[in]   info     #3: load index information of the cluster
 *
 * @retval      0        #1: no load exceed schedule threshold
 * @retval      >0       #2: index of load which exceed schedule threshold
 *
 * @note        This function should be called during getting ready node
 *              of the schedulig job. Returning index of a load means the node
 *              is not ready for dispatching the job.
 */
int
threshold_check(double *load, float *thresh, struct quickpoolInfo *info)
{
    int reason = 0;

    unsigned int i;
    for (i = 0; i < info->num_index; i++) {

        if (INFINIT_LOAD <= load[i] || -INFINIT_LOAD >= load[i]
            || INFINIT_LOAD <= thresh[i] || -INFINIT_LOAD >= thresh[i]) {
            continue;
        }

        if (INCR == info->resource_table[i].order_type) {
            if (load[i] > thresh[i]) {
                reason = i + REASON_NODE_LOAD;
                break;
            }
        } else {
            if (load[i] < thresh[i]) {
                reason = i + REASON_NODE_LOAD;
                break;
            }
        }
    }

    return reason;
} // end function threshold_check

#ifdef __cplusplus
}
#endif
